package atm;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

/**
 * 登录
 */
public class LoginPanel extends JPanel implements ActionListener {
    private AtmGUI atmGUI;
    private int error = 3;
    private JLabel accountLabel = new JLabel("账号：");
    private JLabel pwdLabel = new JLabel("密码：");
    private JTextField accountTextField = new JTextField();
    private JPasswordField pwdField = new JPasswordField();
    private JButton loginBtn = new JButton(new ImageIcon("images\\buttonLand.jpg"));
    private Font font = new Font("宋体", Font.BOLD, 25);

    public LoginPanel(AtmGUI atmGUI) {
        this.atmGUI = atmGUI;
        this.setLayout(null);
        accountLabel.setBounds(300, 200, 80, 50);
        accountLabel.setFont(font);
        accountTextField.setBounds(380, 200, 250, 50);
        accountTextField.setFont(font);
        pwdLabel.setBounds(300, 250, 80, 50);
        pwdLabel.setFont(font);
        pwdField.setBounds(380, 250, 250, 50);
        pwdField.setFont(font);
        loginBtn.setBounds(420, 320, 140, 50);
        loginBtn.addActionListener(this);
        this.add(accountLabel);
        this.add(accountTextField);
        this.add(pwdLabel);
        this.add(pwdField);
        this.add(loginBtn);

        this.setOpaque(false);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        ArrayList<User> users = atmGUI.getUsers();
        String account = accountTextField.getText();
        String pwd = new String(pwdField.getPassword());
        for (int i = 0; i < users.size(); i++) {
            if (users.get(i).getAccount().equals(account) && users.get(i).getPwd().equals(pwd)) {
                atmGUI.showPanel("menu");
                return;
            }
        }
            error--;
            if (error > 0){
                JOptionPane.showMessageDialog(this,"登录失败，还剩" + error + "次");
            }else {
                JOptionPane.showMessageDialog(this,"登录失败，卡已被吞");
                System.exit(0);
            }
        accountTextField.setText("");
        pwdField.setText("");
    }
}